package cn.broadmax.boot.modules.test.controller;

import cn.broadmax.boot.base.message.SResult;
import cn.broadmax.boot.util.JsonUtils;
import io.swagger.annotations.Api;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.context.annotation.Profile;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;

/**
 * @author jeff
 * @date 2020/6/25
 */
@Api(tags = "validate测试")
@Slf4j
@Profile("dev")
@Validated
@RestController
@RequestMapping("/test/validate")
public class ValidateTestController {

    @GetMapping("/test1")
    public SResult<Object> test1(@NotEmpty(message="username不能为空") String username)  {
        return SResult.ok(username);
    }

    @PostMapping("/test2")
    public SResult<Object> test2(@Validated @RequestBody TestBean bean)  {
        return SResult.ok(JsonUtils.toJSONString(bean));
    }

    @Data
    protected static class TestBean{
        @NotEmpty(message = "userName不能为空")
        private String userName;

        @Email(message = "邮箱异常")
        private String email;

        @Range(message = "年龄必须在10到99", min = 10, max = 99)
        private Integer age;
    }

}
